﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebTest.Infrastructure.Interfaces;

namespace WebTest.WebApi.Filter
{
    public class GlobalExceptionFilterAsync : IAsyncExceptionFilter
    {
        private readonly INLogService logService;

        public GlobalExceptionFilterAsync(INLogService logService)
        {
            this.logService = logService ?? throw new ArgumentNullException(nameof(logService));
        }
        public Task OnExceptionAsync(ExceptionContext context)
        {
            if (!context.ExceptionHandled)
            {
                logService.LogError(context.Exception);

                context.ExceptionHandled = true;
            }

            return Task.CompletedTask;
        }
    }
}
